<template>
  <div class="app-container">
    <el-form :model="queryParams" :inline="true" label-width="68px">
      <el-row>
        <el-form-item label="套餐名称">
          <el-input
            v-model="queryParams.mealName"
            placeholder="请输入套餐名称"
            clearable
            size="small"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="服务单位">
          <el-select v-model="queryParams.orgId" placeholder="请选择服务单位">
            <el-option
              v-for="item in fetchCityListOptions"
              :key="item.codevalue"
              :label="item.codename"
              :value="item.codevalue"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
        </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          s
          style="font-weight: bold"
          @click="addOrder"
          >添加套餐</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Edit"
          style="font-weight: bold"
          @click="updateOrder"
          :disabled="single"
          >修改套餐</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        align="center"
        width="55"
      ></el-table-column>

      <el-table-column
        label="套餐编码"
        prop="mealCode"
        align="center"
        width="150"
      />
      <el-table-column label="套餐名称" prop="mealName" align="center" />
      <el-table-column
        label="所属服务单位"
        align="center"
        prop="orgName"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="套餐内容"
        align="center"
        prop="orderType"
        :show-overflow-tooltip="true"
      >
        <template v-slot="scope">
          <el-button link @click="check(scope.row.id)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="套餐价格"
        align="center"
        prop="amount"
        :show-overflow-tooltip="true"
      />

      <el-table-column label="状态" align="center" key="status">
        <template v-slot="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row.id, 1)"
            >启用</el-button
          >

          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row.id, 0)"
            >停用</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="套餐内容" v-model="dialogVisible">
      <el-table
        show-summary
        :summary-method="getSummaries"
        :data="mealLinesData"
        border
      >
        <el-table-column prop="matName" label="商品名称" align="center" />
        <el-table-column prop="spec" label="商品规格" align="center" />
        <el-table-column prop="unit" label="单位" align="center" />
        <el-table-column prop="matNum" label="数量" align="center" />
        <el-table-column prop="price" label="单价" align="center" />
        <el-table-column prop="amount" label="金额" align="center" />
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="dialogVisible = false"
            >确 定</el-button
          >
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { fetchCityList } from '@/api/order/order'
import {
  listTable,
  getMealLineList,
  statusUpdate
} from '@/api/erp/packageManagement'
import { reactive, toRefs } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const data = reactive({
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 对话框
  dialogVisible: false,
  // 遮罩层
  loading: true,
  // 总条数
  total: 0,
  //服务单位列表
  fetchCityListOptions: [],
  // 表数据
  tableList: [],
  // 套餐内容表数据
  mealLinesData: [],
  // 查询参数
  queryParams: {
    mealName: '', // - 套餐名称
    orgId: '', // 所属服务单位
    pageNum: 1,
    pageSize: 10
  }
})
let {
  ids,
  single,
  dialogVisible,
  loading,
  total,
  fetchCityListOptions,
  tableList,
  mealLinesData,
  queryParams
} = toRefs(data)
init()
function init() {
  getfetchCityList()
  getList()
}
// 获取服务单位列表
function getfetchCityList() {
  loading.value = true
  fetchCityList().then(response => {
    fetchCityListOptions.value = response.data
    loading.value = false
  })
}

// 查询套餐列表
function getList() {
  loading.value = true
  listTable(queryParams.value).then(response => {
    tableList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
// 多选框选中数据
function handleSelectionChange(selection) {
  single.value = selection.length != 1
  ids.value = selection.map(item => item.id)
}
// 搜索按钮操作
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

// 添加套餐按钮
function addOrder() {
  router.push({ path: '/xs/tjtc', query: { flag: 'addOrder' } })
}
// 修改套餐按钮
function updateOrder() {
  router.push({
    path: '/xs/xgtc',
    query: { flag: 'updateOrder', id: ids.value[0] }
  })
}

// 查看按钮
function check(mealId) {
  getMealLineList({ mealId }).then(res => {
    console.log(res)
    if (res.code == 200) {
      dialogVisible.value = true
      mealLinesData.value = res.data
    } else ElMessage.error(res.msg)
  })
}

// 启用/停用
function handleStatusChange(row) {
  let text = row.status === '0' ? '停用' : '启用'
  ElMessageBox.confirm(
    '确认要"' + text + '""' + row.mealName + '"套餐吗?',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(function () {
      return statusUpdate({ id: row.id, status: row.status })
    })
    .then(() => {
      ElMessage.msgSuccess(text + '成功')
    })
    .catch(function () {
      row.status = row.status === '0' ? '1' : '0'
    })
}

function getSummaries(param) {
  const { columns, data } = param
  const sums = []
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = '合计'
      return
    }
    const values = data.map(item => Number(item[column.property]))
    if (column.property == 'amount') {
      sums[index] = values.reduce((prev, curr) => {
        const value = Number(curr)
        if (!isNaN(value)) {
          return prev + curr
        } else {
          return prev
        }
      }, 0)
    }
  })

  return sums
}
</script>
